package com.leoman.dao;

import com.leoman.entity.VideoRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 视频投放记录
 * Created by Administrator on 2016/10/18 0018 下午 3:05.
 */
public interface VideoRecordDao extends JpaRepository<VideoRecord, Integer>, JpaSpecificationExecutor<VideoRecord> {

    @Query("select a from VideoRecord a where a.video.isShopping = 1 and a.video.checkStatus = 1 and a.video.recordStatus = 1 and ((a.endTime is null and a.startTime is not null) or (a.sendType = 4 and a.startTime <= ?1 and a.endTime > ?1)) order by a.startTime desc")
    Page<VideoRecord> iPageForAppNothing(Long nowTime, Pageable pageable);

    @Query("select a from VideoRecord a where a.regionIds like ?1 and a.video.isShopping = 1 and a.video.checkStatus = 1 and a.video.recordStatus = 1 and ((a.endTime is null and a.startTime is not null) or (a.sendType = 4 and a.startTime <= ?2 and a.endTime > ?2)) order by a.startTime desc")
    Page<VideoRecord> iPageForAppOnlyRegionId(String regionId, Long nowTime, Pageable pageable);

    @Query("select a from VideoRecord a where a.video.videoType.id = ?1 and a.video.isShopping = 1 and a.video.checkStatus = 1 and a.video.recordStatus = 1 and ((a.endTime is null and a.startTime is not null) or (a.sendType = 4 and a.startTime <= ?2 and a.endTime > ?2)) order by a.startTime desc")
    Page<VideoRecord> iPageForAppOnlyTypeId(Integer typeId, Long nowTime, Pageable pageable);

    @Query("select a from VideoRecord a where a.regionIds like ?1 and a.video.videoType.id = ?2 and a.video.isShopping = 1 and a.video.checkStatus = 1 and a.video.recordStatus = 1 and ((a.endTime is null and a.startTime is not null) or (a.sendType = 4 and a.startTime <= ?3 and a.endTime > ?3)) order by a.startTime desc")
    Page<VideoRecord> iPageForAppWithRegionIdAndTypeId(String regionId, Integer typeId, Long nowTime, Pageable pageable);

    @Query("select a from VideoRecord a where a.video.id = ?1 order by a.startTime desc")
    List<VideoRecord> findListByVideoId(Integer videoId);

    @Query("select a from VideoRecord a where a.video.id = ?1 order by a.startTime desc")
    Page<VideoRecord> pageByVideoId(Integer videoId, Pageable pageable);

    @Query("select a from VideoRecord a where a.video.videoType.id = ?1 and a.video.isShopping = 1 and a.video.checkStatus = 1 and a.video.recordStatus = 1 and a.startTime is not null and ((a.endTime is null and a.startTime is not null) or (a.sendType = 4 and a.startTime <= ?2 and a.endTime > ?2)) order by a.startTime desc")
    List<VideoRecord> findListByVideoTypeId(Integer videoTypeId, Long nowTime);
}